package com.enjoyor.health.HGPT.web.rest;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.enjoyor.health.HGPT.core.config.Constant;
import com.enjoyor.health.HGPT.core.entity.DialogContent;
import com.enjoyor.health.HGPT.core.entity.DialogRecord;
import com.enjoyor.health.HGPT.service.BizConversationService;
import com.enjoyor.health.HGPT.web.CodeEnum;
import com.enjoyor.health.HGPT.web.RetResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author Zhu Min
 * @date 2023/10/20 11:05
 */
@Api(value = "对话", tags = {"对话"})
@RestController
public class ConversationController {

    @Autowired
    private BizConversationService bizConversationService;

    /**
     *  历史记录
     */
    @RequestMapping(value = "/aiHealthReport/dialogRecord", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult miniappDialogRecord(String dialogId){
        List<DialogContent> dialogContentList = bizConversationService.getDialogContentById(dialogId);
        JSONArray result = new JSONArray();
        for (DialogContent dialogContent : dialogContentList) {
            JSONObject o = new JSONObject();
            o.put("question", dialogContent.getQuestion());
            o.put("content", dialogContent.getShowContent());
            o.put("time", dialogContent.getShowTime() == null? "": LocalDateTimeUtil.format(dialogContent.getShowTime(), "yyyy-MM-dd HH:mm:ss"));
            o.put("statement", dialogContent.getShowStatement());
            o.put("title", dialogContent.getShowTitle());
            o.put("url", dialogContent.getShowUrl());
            result.add(o);
        }
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

    /**
     * 开始体验（新建对话）,建立一个新的聊天
     */
    @RequestMapping(value = "/aiHealthReport/conversationNew", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getTalk(String openId){
        String uuid = IdUtil.simpleUUID();
        bizConversationService.getDbTalk(uuid, openId);

        JSONObject result = new JSONObject();
        result.put("dialogId", uuid);
        result.put("resultSimple", new JSONArray("展会体验包","我的健康报告", "血管粥样硬化的形成（图文）", "原发性高血压的危害（视频）", /*"神经衰弱测评",*/ "抑郁测评"));
        result.put("time", LocalDateTimeUtil.format(LocalDateTime.now(), Constant.DATE_TIME_FORMAT));
        result.put("title", "您好！我是健康宝");
        result.put("content", "您的健康小管家，为您提供相关的医疗健康信息查询。");
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

    /**
     *   对话 记录聊天记录
     */
    @RequestMapping(value = "/aiHealthReport/conversation", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getQATalk(String dialogId, String question, String openId){
        return new RetResult().setResult(bizConversationService.getAnswerFromAI(question,openId,dialogId));
    }

    /**
     * 获取历史聊天列表
     */
    @RequestMapping(value = "/aiHealthReport/dialogList", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getChatHistory(String openId){
        JSONObject data = new JSONObject();
        data.put("data", bizConversationService.dialogList(openId));
        return new RetResult().of(CodeEnum.SUCCESS).setResult(data);
    }

    /**
     * 对话评价-点赞点踩
     */
    @RequestMapping(value = "/aiHealthReport/conversationEvaluation", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult conversationEvaluation(String openId, String dialogId, String number, String evaluation, String type, String comment){
        bizConversationService.updateDialogEvaluation(evaluation, type, comment, dialogId, number);
        return new RetResult().of(CodeEnum.SUCCESS);
    }

    /**
     * # 修改聊天信息
     * # 1.编辑对话标题
     */
    @RequestMapping(value = "/aiHealthReport/dialogInfoModify", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult modifyChat(String openId, String dialogId, String nameNew){
        bizConversationService.updateDialogName(nameNew, dialogId);
        return new RetResult().of(CodeEnum.SUCCESS);
    }

    /**
     * # 2.删除聊天对话
     * # 根据传入的‘chat_id’，查表‘dialog_record’对应‘id’，将’state=1‘
     * # 输入‘chat_id’、‘delete_chat’，‘chat_id’为删除的历史聊天记录id，‘delete_chat’为‘1’，删除对应历史记录，成功返回200响应码，失败返回-1响应码。
     */
    @RequestMapping(value = "/aiHealthReport/dialogDelete", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult deleteChat(String openId, String dialogId){
        bizConversationService.updateDialogState(2, dialogId);
        return new RetResult();
    }

}
